<?php
class SimplePageMaker extends object {
	var $page ;
	var $all ;
	var $holder = array();
	var $pageholder = array() ;
	var $items = 10 ;
	var $link_active = 'color: red;' ;
	var $link_inactive = '' ;
	var $output = '' ;
	var $nextpage = false ;
	var $prevpage = false ;
	var $chunked ;
	var $currentpage = false ;
	var $delimiter = ' :: ' ;
	var $vertical_delimiter = '<br>' ;
	var $file=_ME ;
	var $requestname = 'page' ;
	var $requesttail = '' ;
	var $shift_prev = '&lt;&lt;' ;
	var $shift_next = '&gt;&gt;' ;
	var $itemsperpage = 3 ;
	var $show_rewinds = false ;
	var $rewind_first = '&lt;&lt;&lt;' ;
	var $rewind_last = '&gt;&gt;&gt;' ;

	function SimplePageMaker($all,$page) {
		if(!function_exists('array_chunk')||!function_exists('array_key_exists')) {
			trigger_error(sprintf('SimplePageMaker::constructor() - sorry, your php version is too old and I am too lazy to paginate :)'),E_USER_ERROR) ;
		} // if
		$this->all = $all ;
		$this->page = $page ? $page : 1 ;
	} // end_f-n
	function calculate() {
		if(!$this->all) {
			return ;
		} // if
		for($i=0; $i<$this->all; $i++) {
			$this->holder[$i+1] = $i ;
		} // for
		$this->chunked = array_chunk($this->holder,$this->itemsperpage,true) ;
		$this->pages=array_keys($this->chunked);
		$this->chunks = array_chunk($this->pages,$this->items,true);
		for($i=0; $i<count($this->chunks); $i++) {
			if(array_key_exists($this->page-1,$this->chunks[$i])) {
				$this->currentpage = $i ;
				break;
			} // if
		} // for
		if(isset($this->chunks[$this->currentpage-1])) {
			$c = array_keys($this->chunks[$this->currentpage-1]);
			$this->prevpage = $c[count($c)-1];
		} // if
		if(isset($this->chunks[$this->currentpage+1])) {
			$c = array_keys($this->chunks[$this->currentpage+1]);
			$this->nextpage = $c[0];
		} // if
	} // end_f-n
	function make() {
		$this->calculate();
		if(!$this->all) {
			return ;
		} // if
		if(!array_key_exists($this->page-1,$this->pages)) {
			trigger_error(sprintf('SimplePageMaker::make() - out of bounds in page %s',$this->page),E_USER_ERROR) ;
		} // if
		if($this->show_rewinds && count($this->pages)>$this->items) {
			$this->output .= 
				'<a href="'.$this->file.'?'.$this->requestname.'=1'. $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->rewind_first . '</a>' . $this->delimiter ;
			if($this->prevpage) {
				$this->output .= 
					'<a href="'.$this->file.'?'.$this->requestname.'='.($this->prevpage+1). $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->shift_prev . '</a>' . $this->delimiter ;
			} // if	
			else {
				$this->output .= 
				'<a href="'.$this->file.'?'.$this->requestname.'=1'. $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->shift_prev . '</a>' . $this->delimiter ;
			} // else
		} // if
		else {
			if($this->prevpage) {
				$this->output .= 
					'<a href="'.$this->file.'?'.$this->requestname.'='.($this->prevpage+1). $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->shift_prev . '</a>' . $this->delimiter ;
			} // if	
		} // else
		foreach($this->chunks[$this->currentpage] as $k=>$v) {
			$delimiter = isset($this->chunks[$this->currentpage][$k+1]) || $this->nextpage ? $this->delimiter : '' ;
			if($this->page == $k+1) {
				$this->output .= 
					'<span style="'. $this->link_active . '">' . ($k+1) . 
					'</span>' . $delimiter ;
				continue ;
			} // if
			$this->output .= 
					'<a href="'.$this->file.'?'.$this->requestname.'='.($k+1). $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . ($k+1) . 
					'</a>' . $delimiter ;
		} // foreach
		if($this->show_rewinds && count($this->pages)>$this->items) {
			$c = array_keys($this->pages);
			$last = $this->pages[$c[count($c)-1]] + 1 ;
			if($this->nextpage) {
				$this->output .= 
					'<a href="'.$this->file.'?'.$this->requestname.'='.($this->nextpage+1). $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->shift_next . '</a>'  ;
			} // if
			else {
				$this->output .=  $this->delimiter
					. '<a href="'.$this->file.'?'.$this->requestname.'='.$last. $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->shift_next . '</a>'  ;
			} // else
			$this->output .= $this->delimiter 
					. '<a href="'.$this->file.'?'.$this->requestname.'='.$last. $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->rewind_last . '</a>'  ;
		} // if
		else {
			if($this->nextpage) {
				$this->output .= 
					'<a href="'.$this->file.'?'.$this->requestname.'='.($this->nextpage+1). $this->requesttail. 
					'" style="'. $this->link_inactive . '">' . $this->shift_next . '</a>'  ;
			} // if
		} // else
	} // end_f-n
	function get_range() {
		return array_slice($this->holder,($this->page-1)*$this->itemsperpage,$this->itemsperpage);
	} // end_f-n
	function show() {
		return $this->output ;
	} // end_f-n
	function show_vertical() {
		return str_replace($this->delimiter,$this->vertical_delimiter,$this->output) ;
	} // end_f-n
} // end_class	 
?>